define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'vue'], function ($, undefined, Backend, Table, Form, Vue) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'coupons/index' + location.search,
                    add_url: 'coupons/add',
                    edit_url: 'coupons/edit',
                    del_url: 'coupons/del',
                    multi_url: 'coupons/multi',
                    import_url: 'coupons/import',
                    table: 'coupons',
                }
            });

            var table = $("#table");


            // 初始化表格
            table.bootstrapTable({
                commonSearch: false,
                showExport: false,
                showToggle: false,
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'bg_image', title: __('Bg_image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'orginal_price', title: __('Orginal_price'), operate:'BETWEEN'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons :[
                                {
                                    name: 'push_coupon',
                                    text: '批量发放',
                                    title: '批量发放',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-jpy',
                                    extend: 'data-area=\'["70%", "80%"]\'',
                                    url: function (row) {
                                        return `coupons/push?coupon_id=${row.id}`;
                                    }
                                },
                                {
                                    name: 'push_coupon',
                                    text: '单人发放',
                                    title: '单人发放',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-jpy',
                                    extend: 'data-area=\'["70%", "80%"]\'',
                                    url: function (row) {
                                        return `coupons/push_single?coupon_id=${row.id}`;
                                    }
                                },
                            ],
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        push: function () {
            new Vue({
                el: "#coachDetail",
                data() {
                    return {
                        userInfo:{},//绑定会员
                    }
                },
                methods: {
                    selectUser(){
                        var that = this;
                        parent.Fast.api.open("xykeep/user/select?multiple=false", __('选择'), {
                            area: ['50%', '50%'],
                            callback: function (data) {
                                that.userInfo = data
                            }
                        });
                    },
                },
            });
            Controller.api.bindevent();
        },
        push_single: function () {
            new Vue({
                el: "#coachDetail",
                data() {
                    return {
                        userInfo:{},//绑定会员
                    }
                },
                methods: {
                    selectUser(){
                        var that = this;
                        parent.Fast.api.open("xykeep/user/select?multiple=false", __('选择'), {
                            area: ['50%', '50%'],
                            callback: function (data) {
                                that.userInfo = data
                            }
                        });
                    },
                },
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
